<?php

class Calc_pdf extends FPDF
{
    private $a_DS;
    private $a_filial;
    private $a_manager;
    private $a_tourCurrency;
    private $a_managerCommision;
    private $a_finPR;
    private $a_finFO;
    
    public function SetDS($p_DS){
		$this->a_DS = $p_DS;
    }
    
    public function SetFilial($p_filial){
	$this->a_filial = $p_filial;
    }

    public function SetManager($p_manager){
	$this->a_manager = $p_manager;
    }

    public function SetManagerCommision($p_commisionValue){
		$this->a_managerCommision = $p_commisionValue;
    }
    
    public function SetFinPR($p_docPR){
		$this->a_finPR = $p_docPR;
    }

    public function SetFinFO($p_docFO){
		$this->a_finFO = $p_docFO;
    }
    
    public function SetTourCurrency($p_currency){
    	$this->a_tourCurrency = $p_currency;
    }
    
    public function GetCalc(){
		$this->AddPage();
		$this->AddFont('ArialMT','','arial.php');
		$this->AddFont('ArialBD','','arialbd.php');
		$this->CalcTitle();
		$this->Table1();
		$this->Table2();
		$this->Table3();
		$this->Output();
    }    
    
    private function CalcTitle(){
        $this->SetFont('ArialBD','',8);	
        $this->Cell(80);
        //Title
        $this->SetXY(5,5);
        $str = "Додаток до Договору на туристичне обслуговування  №{$this->a_DS[0]->agreement}  вiд {$this->a_DS[0]->date_doc}р.";
        $this->Cell(0,5,iconv('UTF-8', 'Windows-1251', $str));
		//Line break
		$this->SetFont('ArialMT','',10);	
        $this->Ln(8);
		$str =	"Документ для внутрiшнього користування";
        $this->Cell(0,4,iconv('UTF-8', 'Windows-1251', $str), 0, 1, 'R');
        $this->Ln(6);
		$str =	"Україна, м.{$this->a_filial->city_name}, вул.{$this->a_filial->adress},  т/ф {$this->a_filial->phones}, e-mail: {$this->a_filial->email}";
        $this->Cell(0,4,iconv('UTF-8', 'Windows-1251', $str), 0, 1, 'L');
        $this->Ln(6);
        $str = "КАЛЬКУЛЯЦIЯ ТУРУ";
        $this->Cell(0,5,iconv('UTF-8', 'Windows-1251', $str), 0, 1, 'C');
    }
    
    public function Table1(){
        $str = "ТУРОПЕРАТОР / КРАЇНА";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 1, 0, 'L');
        $str = $this->a_DS[0]->touroperator_name.'/'.$this->a_DS[0]->country_name;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'TBR', 0, 'L');
    	$this->Ln();

        $str = "Вартість туру в іноземній валюті";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LRB', 0, 'L');
        $str = "{$this->a_DS[0]->summ_tour}";
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();
    	
        $str = "Валюта";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = $this->a_tourCurrency;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();
    	
        $str = "Курс валют";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = $this->a_DS[0]->curr_cource;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();

        $str = "Знижка, %";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = $this->a_DS[0]->discount_per;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();
    	
        $str = "Знижка, фикс.";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = $this->a_DS[0]->discount_fix;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();

        $str = "Загальна вартість туру у валюті";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
    	$tourSummInCurrency = round(($this->a_DS[0]->summ_tour*(1-$this->a_DS[0]->discount_per/100)-$this->a_DS[0]->discount_fix)*(1+$this->a_DS[0]->touroperator_koeff/100),2);
        $str = $tourSummInCurrency;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();

        $str = "Вартість, яка перераховуеться на р/р";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = round($tourSummInCurrency*$this->a_DS[0]->curr_cource, 2);
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();
    	
        $str = "Банківські послуги";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = round($tourSummInCurrency*$this->a_DS[0]->curr_cource*$this->a_DS[0]->bank_s, 2);
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();

        $str = "Загальна вартість для туриста";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = round($tourSummInCurrency*$this->a_DS[0]->curr_cource*(1+$this->a_DS[0]->bank_s), 2);
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln(15);
    }
    
    public function Table2(){
        $str = "Розрахунок комісій";
        $this->Cell(0,5,iconv('UTF-8', 'Windows-1251', $str), 0, 1, 'C');
    	$this->Ln(2);
    	$tourSummInCurrency = round(($this->a_DS[0]->summ_tour*(1-$this->a_DS[0]->discount_per/100)-$this->a_DS[0]->discount_fix)*(1+$this->a_DS[0]->touroperator_koeff/100),2);
        $str = "Вартість, яка перераховуеться на р/р";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LRBT', 0, 'L');
        $str = round($tourSummInCurrency*$this->a_DS[0]->curr_cource, 2);
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BRT', 0, 'L');
    	$this->Ln();
    	
        $str = "Сума по рахунку від туроператора";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = $this->a_DS[0]->order_summ;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();

        $str = "Комісія від продажу туру(загальна)";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = round($tourSummInCurrency*$this->a_DS[0]->curr_cource, 2) - $this->a_DS[0]->order_summ;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();
    	
        $str = "Комісія менеджера, %";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = $this->a_managerCommision;
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln();

        $str = "Комісія менеджера, сума";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
        $str = round(($tourSummInCurrency*$this->a_DS[0]->curr_cource - $this->a_DS[0]->order_summ)*($this->a_managerCommision/100), 2);
    	$this->Cell(125,5,iconv('UTF-8', 'Windows-1251', $str), 'BR', 0, 'L');
    	$this->Ln(15);
    }

    public function Table3(){
        $str = "Передплата (сума, дата)";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBRT', 0, 'L');
    	$summ = '';
    	$date = '';
    	if($this->a_finPR){
    		$summ = $this->a_finPR[0]->summ_value;
    		$date = implode('.', array_reverse(explode('-', $this->a_finPR[0]->oper_date)));
    	}
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $summ), 'BRT', 0, 'L');
    	$this->Cell(60,5,iconv('UTF-8', 'Windows-1251', $date), 'BRT', 0, 'L');
    	$this->Ln();

        $str = "Кінцевий розрахунок (сума, дата)";
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $str), 'LBR', 0, 'L');
    	$summ = '';
    	$date = '';
    	if($this->a_finFO){
    		$summ = $this->a_finFO[0]->summ_value;
    		$date = implode('.', array_reverse(explode('-', $this->a_finFO[0]->oper_date)));
    	}
    	$this->Cell(65,5,iconv('UTF-8', 'Windows-1251', $summ), 'BRT', 0, 'L');
    	$this->Cell(60,5,iconv('UTF-8', 'Windows-1251', $date), 'BRT', 0, 'L');
    	$this->Ln();
    }
    
}
?>